package com.ruoyi.demo.service.base.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.demo.domain.DemoStudent;
import com.ruoyi.demo.mapper.DemoStudentMapper;
import com.ruoyi.demo.service.base.DemoStudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 学生信息Service业务层处理
 *
 * @author zhangshu
 * @date 2021-01-25
 */
@Service
public class DemoStudentServiceImpl extends ServiceImpl<DemoStudentMapper, DemoStudent> implements DemoStudentService {
    @Autowired
    private DemoStudentMapper demoStudentMapper;

    @Override
    @DataScope(deptAlias = "d",userAlias = "u")
    public List<DemoStudent> findByAll(DemoStudent demoStudent) {
        return demoStudentMapper.findByAll(demoStudent);
    }

    @Override
    @DataScope(deptAlias = "d",userAlias = "u")
    public List<DemoStudent> queryList(DemoStudent demoStudent) {
        LambdaQueryWrapper<DemoStudent> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(demoStudent.getStudentName())){
            lqw.like(DemoStudent::getStudentName ,demoStudent.getStudentName());
        }
        if (demoStudent.getStudentAge() != null){
            lqw.eq(DemoStudent::getStudentAge ,demoStudent.getStudentAge());
        }
        if (StringUtils.isNotBlank(demoStudent.getStudentHobby())){
            lqw.eq(DemoStudent::getStudentHobby ,demoStudent.getStudentHobby());
        }
        if (StringUtils.isNotBlank(demoStudent.getStudentSex())){
            lqw.eq(DemoStudent::getStudentSex ,demoStudent.getStudentSex());
        }
        if (StringUtils.isNotBlank(demoStudent.getStudentStatus())){
            lqw.eq(DemoStudent::getStudentStatus ,demoStudent.getStudentStatus());
        }
        if (demoStudent.getStudentBirthday() != null){
            lqw.eq(DemoStudent::getStudentBirthday ,demoStudent.getStudentBirthday());
        }
        return this.list(lqw);
    }

    /**
     * @description 导入用户数据
     * @param userList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return java.lang.String
     * @author zhangshu
     * @date 2021-01-27 16:03
     */
    @Override
    public String importUser(List<DemoStudent> userList, Boolean isUpdateSupport, String operName)
    {
        if (com.ruoyi.common.utils.StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (DemoStudent user : userList)
        {
            try
            {
                // 验证是否存在这个用户
                LambdaQueryWrapper<DemoStudent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(DemoStudent::getStudentName,user.getStudentName());
                DemoStudent u = demoStudentMapper.selectOne(lambdaQueryWrapper);
                //查询不到用户,说明数据库里没有该用户
                if (com.ruoyi.common.utils.StringUtils.isNull(u))
                {
                    this.save(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、学生： " + user.getStudentName() + " 导入成功");
                }
                //用户存在,允许更新操作
                else if (isUpdateSupport)
                {
                    user.setStudentId(u.getStudentId());
                    this.updateById(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、学生： " + user.getStudentName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、学生： " + user.getStudentName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、学生： " + user.getStudentName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
